// ==UserScript==
// @name           Google bar myself
// @description    Google bar myself for Greasemonkey Javascript
// @version        1.0.0
// @date           2011-04-12
// @author         Osamu Tanaka
// @namespace      http://code.google.com/p/google-bar-myself/
// @include        http*://mail.google.com/*
// @include        http*://www.google.com/calendar*
// @include        http*://docs.google.com/*
// @include        http*://sites.google.com/*
// @include        http*://video.google.com/*
// ==/UserScript==

chrome.extension.sendRequest({elements: "o"}, function(response) {
    var DEBUG_MODE = false;
    var cP = null;
    var initstart = false;

    //---- CHECK PAGE LOAD ----//
    function getLoad() {
        return document.getElementsByClassName('cP');
    }
    function init() {
        debugLog("init start");
        if (getLoad()[0] == cP) { gbarconv(); }
        if (cP != null)
            cP.removeEventListener("DOMSubtreeModified", run, false);
        if ((cP = getLoad()[0]) != null) {
            cP.addEventListener("DOMSubtreeModified", run, false);
        }
    }
    //---- END CHECK PAGE LOAD ----//

    function run() {
        gbarconv();
    }

    function gbarconv() {
        initstart = true;
        debugLog("gbar customizing start");
        if (document.getElementById("gbar")) {
            try {
                var obj = document.getElementById("gbar").childNodes[0];
				debugLog("obj.name:" + obj.name);
                if (obj.name == "") {
                    obj = document.getElementById("gbar");
                }
                var elements = obj.getElementsByClassName("gb1");
                for (var i = elements.length - 1; i >= 0; i--) {
                    debugLog("elements.target:" + elements[i].target);
                    debugLog("element:" + elements[i]);
                    elements[i].target = "_top";
                };
            }
            catch (e) {
                debugLog(e);
            }
        }
    }

    function debugLog(str){
        if (DEBUG_MODE){
          if (typeof(GM_log) != 'function' && typeof(opera) != 'undefined') {
            GM_log = opera.postError; // for opera
          }
          GM_log(str);
        }
      }

    window.addEventListener("DOMSubtreeModified", init, false);
    if (!initstart) {
        gbarconv();
    }

});